/**
 *
 */
package com.suieqian.util;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @Description: 用于将要返回给ajax异步请求的数据封装成对象
 * @author: tianhua.xie
 * @Date: 2017年3月5日 下午2:11:04
 *
 */
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class Message<T> {

    public static final String SUCCESS = "200";

    public static final String ERROR = "500";

    public static final String HTTP_ERROR = "405";

    public static final String Auth_ERROR = "401";

    /**
     * 参数异常
     */
    public static final String PARAMETER_ERROR = "4995";


    /** 200: 成功 500:失败 */
    @ApiModelProperty(value = "200: 成功 500:失败")
    private String code;

    /** 信息提示 */
    @ApiModelProperty(value = "信息提示")
    private String message;

    /** 返回的数据 */
    @ApiModelProperty(value = "返回的数据")
    private T data;

    private String requestId;

    public Message(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Message(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Message<T> success(T data) {
        return new Message(SUCCESS, "OK", data);
    }

    public static <T> Message<T> success() {
        return new Message(SUCCESS, "OK", null);
    }

    public static <T, R> Message<Message.PageResp<R>> success(IPage<T> result, Class<R> clazz) {
        return success(result, null, clazz);
    }

    public static <T, R> Message<Message.PageResp<R>> success(IPage<T> result, String requestId, Class<R> clazz) {
        return success(result, requestId, clazz, null);
    }

    public static <T, R> Message<Message.PageResp<R>> success(IPage<T> result, String requestId, Class<R> clazz, Map<String, Object> extend) {
        Message rsData = new Message();
        PageResp<R> pageResp = new PageResp<>();

        pageResp.setList(result.getRecords().stream().map(it -> BeanUtil.copyProperties(it, clazz)).collect(Collectors.toList()))
                .setPn(Long.valueOf(result.getCurrent()).intValue())
                .setPs(Long.valueOf(result.getSize()).intValue())
                .setExtend(extend)
                .setTotal(Long.valueOf(result.getTotal()).intValue());
        return rsData.setData(pageResp).setRequestId(requestId).setCode(Message.SUCCESS).setMessage("OK");
    }

    public static Message error(String msg) {
        return new Message(ERROR, msg);
    }

    public static Message error(String code, String msg) {
        return new Message(code, msg);
    }

    public static Message httpError(String msg) {
        return new Message(HTTP_ERROR, msg);
    }

    public static Message authError(String msg) {
        return new Message(Auth_ERROR, msg);
    }

    @Data
    @Accessors(chain = true)
    public static class PageResp<T> {
        private int pn;// 页码
        private int ps;// 每页显示记录数
        private int total;// 总共的记录数
        private List<T> list;// 每页显示的数据
        private Map<String, Object> extend; //扩展字段
    }
}
